#!/bin/bash -e

# snp for parallel using segmentation

function info() {
echo Usage: `basename $0` '[-lnsgGacert] reads1.fq.gz reads2.fq.gz'
exit 65
}

while getopts  ":l:p:f:n:s:gGa:ce:r:t:" opts; do
	case  $opts  in
	a) cut=$OPTARG;;
	c) cnv=T;;
	l) interval=$OPTARG;;
	n) nums=$OPTARG;;
	s) sample_name=$OPTARG;;
	g) single_cell=T;;
	G) somatic=T;;
	p) out_prefix=$OPTARG;;
	f) suffix=$OPTARG;;
	e) num_pre=$OPTARG;;
	r) fq_rows=$OPTARG;;
	t) max_task=$OPTARG;;
	*) info;;
	esac
done
shift $(($OPTIND - 1))


if [ $# -lt 2 ]; then info; fi





r1=`rel_abs.sh $1`
r2=`rel_abs.sh $2` # must run before mkdir=true
r12=`echo $r1 $r2`

makedir=true

if test -z "$interval"; then interval=$data_path/intervals/1/b37.bed; fi
if test -z "$num_pre"; then num_pre=400; fi
if test -z "$fq_rows"; then fq_rows=4000000; fi
if test -z "$max_task"; then max_task=40; fi
wait_time=60

. $var

script=$(mktemp $(basename $0).$(date +%m%d%H%M).XXXXXX)
echo \#\!/bin/bash -e >$script

# exec 6>&1
exec 1>>$script
# exec 2>&1

mnt=monitor.sh

fastqstat=fastqstat.sh
fastqc=fastqc.sh
summ=summ.sh

split_fastq=split_fastq.sh

if test -z "$cut"; then
	cutadp=cutadp.sh
elif test "$cut" = "general"; then
	cutadp=cutadp.sh
elif test "$cut" = "malbac"; then
	cutadp=cutadp_malbac.sh
elif test "$cut" = "ctc80"; then
	cutadp=cutadp_ctc80_discard_trimmed.sh
fi

aln=aln_bwa_bt.sh
sam2b=sam2b_samtools.sh
merge=merge_bam.sh
hmmcopy=hmmcopyb38.sh
split_bam=split_bam_0701.sh
bed_split=bed-split.py
extract_bam=extract_bam_bed.sh
extract_bam_bed=extract_bam_bed.sh
markdup=deldup.sh
realn=realign_bt.sh
recal=recal0630.sh
ug=ug.sh
annos=annos0711.sh
combine_vcfs=combine_vcfs.sh
impact=impact.sh
format_simple=format_simple_0629.sh

fq_rename=snpp_fq_rename.sh

lns_file=lns_file.sh
bk=bk.sh
end=end.sh

cpu2=2
cpu4=4
cpu8=8
mem4=4
mem8=8
mem16=16
mem32=32


fq_list=$out_prefix.fq.list.txt
cutadp_list=$out_prefix.cutadp.list.txt
sam_list=$out_prefix.sam.list.txt
bam_list=$out_prefix.bam.list.txt
bed_list=$out_prefix.bed.list.txt
merge_list=$out_prefix.merge.list.txt
split_list=$out_prefix.split.list.txt
markdup_list=$out_prefix.markdup.list.txt

i_bed_list=$out_prefix.i.bed.list.txt
l_bed_list=$out_prefix.l.bed.list.txt
realn_list=$out_prefix.realn.list.txt
recal_list=$out_prefix.recal.list.txt
ug_list=$out_prefix.ug.list.txt
high_list=$out_prefix.high.list.txt
low_list=$out_prefix.low.list.txt
hl_list=$out_prefix.hl.list.txt
combine_list=$out_prefix.combine.list.txt
impact_high_list=$out_prefix.impact.high.list.txt
impact_low_list=$out_prefix.impact.low.list.txt
impact_hl_list=$out_prefix.impact.hl.list.txt
end_list=$out_prefix.end.list.txt

snapshot=$out_prefix.snapshot.txt
test -e $snapshot || echo -e "Sample: $sample_name\nRaw data: $r12\nPanel: $interval\nResults: ${http}/$desti/" >$snapshot

while_pre="while true; do if test \$(qstat|grep \`whoami\`|wc -l) -lt $max_task; then echo"
while_suf="break; else sleep $wait_time; fi; done"


mnt_run='start_id=$(sub -F -nmnt -m1 -p1 $mnt `basename $0` $r12)' # or xy="ab=\$(tes.sh)"

echo; echo $while_pre $mnt_run $while_suf

# fq_stat

fastqstat_run='fqstat_id=$(sub -F -nstat -m8 -p2 $fastqstat -s$sample_name -t2 -p$out_prefix $r12)'
echo; echo $while_pre $fastqstat_run $while_suf

fastqc_run='fqc_id=$(sub -F -nfqc -m8 -p2 $fastqc -t2 -s$sample_name $r12)'
echo; echo $while_pre $fastqc_run $while_suf

# end fq_stat

split_fastq1_run='split1_id=$(sub -F -nsplt1 -m2 -p2 "pigz -cdf $r1|$split_fastq -l$fq_rows -p$out_prefix.1.split -")'
echo; echo $while_pre $split_fastq1_run $while_suf

split_fastq2_run='split2_id=$(sub -F -nsplt2 -m2 -p2 "pigz -cdf $r2|$split_fastq -l$fq_rows -p$out_prefix.2.split -")'
echo; echo $while_pre $split_fastq2_run $while_suf


if qstat ${aln_id[$i]}; then
fq_rename_run='fq_rename_id=$(sub -d"afterok:\$split1_id:\$split2_id" -F -nrenam -m1 -p1 $fq_rename)'
echo; echo $while_pre $fq_rename_run $while_suf

for i in $(seq $num_pre); do
	cutadp_run='cutadp_id[$i]=$(sub -d"afterok:\$fq_rename_id" -F -nadp.$i -m2 -p1 $cutadp -p$out_prefix.$i $out_prefix.1.split.$i.fastq $out_prefix.2.split.$i.fastq)'
	echo; echo $while_pre $cutadp_run $while_suf
done

for i in $(seq $num_pre); do

	while true; do
		if test $(qstat|grep `whoami`|wc -l) -lt $max_task; then
			if qstat ${cutadp_id[$i]}; then
				aln_id[$i]=$(sub -d"afterok:${cutadp_id[$i]}" -F -naln.$i -m6 -p4 $aln -p$out_prefix.$i -s$sample_name -t4 $out_prefix.$i.1.cut.fastq $out_prefix.$i.2.cut.fastq)
				break
			else
				aln_id[$i]=$(sub -F -naln.$i -m6 -p4 $aln -p$out_prefix.$i -s$sample_name -t4 $out_prefix.$i.1.cut.fastq $out_prefix.$i.2.cut.fastq)
				break
			fi
		else
			sleep $wait_time
		fi
	done
	
done

for i in $(seq $num_pre); do
	if qstat \${aln_id[$i]}\; then
	sam2b_id[$i]=$(sub -d"afterok:\${aln_id[$i]}" -F -n2b.$i -m4 -p2 $sam2b -p$out_prefix.$i -t2 $out_prefix.$i.sam)
	else
	sam2b_id[$i]=$(sub -F -n2b.$i -m4 -p2 $sam2b -p$out_prefix.$i -t2 $out_prefix.$i.sam)
	fi
	
	# sam2b_ids=$sam2b_ids:\${sam2b_id[$i]}
done

echo for id in \${sam2b_id[@]}\; do
	echo if qstat \$id\; then
		echo sam2b_ids=\$sam2b_ids:\$id
	echo fi
echo done

chmod 755 $script


merge_id=$(sub -d"afterany$sam2b_ids" -F -nmerg -m16 -p6 $merge -t6 $out_prefix.*.sort.bam) # if there is only one bam file, it goes wrong.

if test -z "$nums"; then nums=120; fi

cat $interval|cut -f1-3 >$out_prefix.bed

$bed_split $out_prefix.bed $nums

for i in $(seq $nums); do

extra_id[$i]=$(sub -d"afterok:$merge_id" -F -nextra.$i -m4 -p2 $extract_bam -p$out_prefix.$i $out_prefix.merge.bam $out_prefix.$i.bed)

mark_id[$i]=$(sub -d"afterok:${extra_id[$i]}" -F -nmark.$i -m4 -p1 $markdup -m -p$out_prefix.$i  $out_prefix.$i.extract.bam)

if test -n "$cnv"; then
del_id[$i]=$(sub -d"afterok:${mark_id[$i]}" -F -ndel.$i -m4 -p1 $markdup -p$out_prefix.$i  $out_prefix.$i.markdup.bam)
del_ids=$del_ids:${del_id[$i]}
fi

realn_id[$i]=$(sub -d"afterok:${mark_id[$i]}" -F -nrealn.$i -m8 -p4 $realn -i$out_prefix.$i.bed -p$out_prefix.$i $out_prefix.$i.markdup.bam)

recal_id[$i]=$(sub -d"afterok:${realn_id[$i]}" -F -nrecal.$i -m8 -p4 $recal -l$out_prefix.$i.bed -p$out_prefix.$i $out_prefix.$i.realn.bam)

ug_id[$i]=$(sub -d"afterok:${recal_id[$i]}" -F -nug.$i -m12 -p6 $ug -i$out_prefix.$i.bed -p$out_prefix.$i $out_prefix.$i.realn.recal.bam)



if test -n "$somatic"; then
	echo; somatic
	if test -n "$single_cell"; then
		single_cell
		# monitors.sh -nano -m12 -c6 -i$l_bed_list "$annos -gm" $ug_list $ug $recal_list &
		ano_id[$i]=$(sub -d"afterok:${ug_id[$i]}" -F -nano.$i -m12 -p6 $annos -gm -l$out_prefix.$i.bed -p$out_prefix.$i $out_prefix.$i.snp.vcf $out_prefix.$i.indel.vcf )
	else
		# monitors.sh -nano -m12 -c6 -i$l_bed_list "$annos -m" $ug_list $ug $recal_list &
		ano_id[$i]=$(sub -d"afterok:${ug_id[$i]}" -F -nano.$i -m12 -p6 $annos -m -l$out_prefix.$i.bed -p$out_prefix.$i $out_prefix.$i.snp.vcf $out_prefix.$i.indel.vcf )
	fi
else
	echo; germline
	if test -n "$single_cell"; then
		single_cell
		# monitors.sh -nano -m12 -c6 -i$l_bed_list "$annos -g" $ug_list $ug $recal_list &
		ano_id[$i]=$(sub -d"afterok:${ug_id[$i]}" -F -nano.$i -m12 -p6 $annos -g -l$out_prefix.$i.bed -p$out_prefix.$i $out_prefix.$i.snp.vcf $out_prefix.$i.indel.vcf )
	else
		# monitors.sh -nano -m12 -c6 -i$l_bed_list $annos $ug_list $ug $recal_list &
		ano_id[$i]=$(sub -d"afterok:${ug_id[$i]}" -F -nano.$i -m12 -p6 $annos -l$out_prefix.$i.bed -p$out_prefix.$i $out_prefix.$i.snp.vcf $out_prefix.$i.indel.vcf )
	fi
fi


# ano_id[$i]=$(sub -d"afterok:${ug_id[$i]}" -F -nano.$i -m12 -p6 $annos -l$out_prefix.$i.bed -p$out_prefix.$i $out_prefix.$i.snp.vcf $out_prefix.$i.indel.vcf )

$out_prefix.$i.high.vcf >> $high_list
$out_prefix.$i.low.vcf  >> $low_list
recal_ids=$recal_ids:${recal_id[$i]}
ano_ids=$ano_ids:${ano_id[$i]}
done

# summ #note that branch should be monitor
merge2_id=$(sub -d"afterany$recal_ids" -F -nmerg2 -m16 -p6 $merge -t6 -p$out_prefix.2 $out_prefix.*.realn.recal.bam)

fqc2_id=$(sub -d"afterok:$merge2_id" -F -nfqc2 -m8 -p2 $fastqc -t2 -s$sample_name $out_prefix.2.merge.bam)

summ_id=$(sub -d"afterok:$merge2_id" -F -nsum -m8 -p2 $summ -g -s$sample_name $out_prefix.2.merge.bam $out_prefix.bed)
# end summ

# hmmcopy #note that branch should be monitor
if test -n "$cnv"; then
merge3_id=$(sub -d"afterany$del_ids" -F -nmerg3 -m16 -p6 $merge -t6 -p$out_prefix.3 $out_prefix.*.deldup.bam)

hmmcopy_id=$(sub -d"afterok:$merge3_id" -F -nhmm -m4 -p1 $hmmcopy $out_prefix.3.merge.bam)
lorenz_id=$(sub -d"afterok:$merge3_id" -F -hlrz -m2 -p1 $lorenz $out_prefix.3.merge.bam)

fi

combh_id=$(sub -d"afterok$ano_ids" -F -ncombh -m4 -p2 $combine_vcfs -p$out_prefix.high $high_list)

combl_id=$(sub -d"afterok$ano_ids" -F -ncombl -m4 -p2 $combine_vcfs -p$out_prefix.low $low_list)


imph_id=$(sub -d"afterok:$combh_id" -F -nimph -m4 -p2 $impact $out_prefix.high.combine.vcf)

impl_id=$(sub -d"afterok:$combl_id" -F -nimpl -m4 -p2 $impact $out_prefix.low.combine.vcf)


fmth_id=$(sub -d"afterok:$imph_id" -F -nfmth -m10 -p1 $format_simple -p$out_prefix.high $out_prefix.high.combine.af.hg19_multianno.vcf)

fmtl_id=$(sub -d"afterok:$impl_id" -F -nfmtl -m10 -p1 $format_simple -p$out_prefix.low $out_prefix.low.combine.af.hg19_multianno.vcf)

lns_id=$(sub -d"afterok:$fmth_id:$fmtl_id" -F -nlns -m1 -p1 $lns_file -p$sample_name *.pdf *.xls $out_prefix.high.anno.info.txt.zip $out_prefix.low.anno.info.txt.zip $out_prefix.summary.txt $out_prefix.interval_summ.txt $out_prefix.low.combine.impact.drugs-prediction.html $out_prefix.high.combine.impact.drugs-prediction.html)

bk_id=$(sub -d"afterok:$lns_id" -F -nbk -m1 -p1 $bk -tsnp/xxx $sample_name.*)

end_id=$(sub -d"afterok:$fmth_id:$fmtl_id:$lns_id:$bk_id" -F -nen -m1 -p1 $end `basename $0` $@)


end
# . $cmd_done
# echo
